import { _decorator, Component, Label, Node, sp } from 'cc';
import { director } from 'cc';
import { assetManager } from 'cc';
import { sys } from 'cc';
import { Vec3, tween } from 'cc';
import { AudioManager } from '../../../common/src/manager/AudioManager';
import { UIManager } from '../../../framework/ui/uimanager/UIManager';
import { GGStore } from '../../../framework/core/base/GGStore';
import { GGAudio } from '../../../framework/core/base/GGAudio';
import DataMgr from '../../../common/src/util/DataMgr';
import Config from '../../../common/src/util/Config';
import { EVENT_NAMES, SETTING_TYPE } from '../../../common/src/util/Enums';
const { ccclass, property } = _decorator;

@ccclass('StartScene')
export class StartScene extends Component {
    @property({ type: Label, displayName: "金币" })
    moneyLabel: Label = null;
    
    @property({ type: Label, displayName: "体力" })
    staminaLabel: Label = null;
    
    @property({ type: Label, displayName: "关卡" })
    levelLabel: Label = null;
    
    // =======================================
    // Component 生命周期
    // =======================================
    protected onLoad(): void {
        this.initListener()
    }

    start() {
        this.updateStartInfo()
    }

    protected onDestroy(): void {
        gg.systemEvent.off(EVENT_NAMES.UPDATE_START_INFO, this.updateStartInfo, this)
    }

    // ----------------- 初始化 ----------------- //
    private initListener() {
        gg.systemEvent.on(EVENT_NAMES.UPDATE_START_INFO, this.updateStartInfo, this)
    }
    
    // =======================================
    // Component 事件
    // =======================================

    // ------------------- 通知事件 ------------------- //
    private updateStartInfo() {
        this.staminaLabel.string = DataMgr.stamina + ""
        this.moneyLabel.string = DataMgr.money + ""
        this.levelLabel.string = "第" + DataMgr.currentLevel + "关"
    }

    // ------------------- 点击事件 ------------------- //
    private onClickSetButton() {
        AudioManager.playButton();
        UIManager.open("setting:res/prefab/SettingDialog", (err, res:Node)=>{
            res.getComponent("SettingDialog").setData({
                type: SETTING_TYPE.START
            })
        })
    }

    //分享按钮
    private onClickShareButton(){
        window.gameSdk.sdk.shareAppMessage({
            positionTag:"分享标记点位",
            callback: (err, res) => {
                if (!err) {
                    console.log("分享成功");
                }else{
                    console.log("分享失败");
                }
            }
        });
        
    }

    // 点击开始游戏
    private onClickPlayButton(){
        assetManager.loadBundle('game', { version: sys.localStorage.getItem('__bundleVersionConfig__') }, (err, bundle) => {
            if (err) { return gg.error(err); }
            
            bundle.loadScene('game', (err, scene) => {
                if (err) { return gg.error(err); }
                director.runScene(scene);
            });
        });
    }

    //排行榜按钮
    private onClickRankButton(){
        AudioManager.playButton();
        UIManager.open("rank:res/prefab/RankDialog")
    }

    //邀请按钮
    private onClickInviteButton(){
        AudioManager.playButton();
        UIManager.open("invite:res/prefab/InviteDialog")
    }

    //添加体力按钮
    private onClickAddStaminButton(){
        AudioManager.playButton();
        UIManager.open("stamina:res/prefab/StaminaDialog")
    }

    //购买金币按钮
    private onClickAddMoneyButton(){
        AudioManager.playButton();
        UIManager.open("money:res/prefab/MoneyDialog")
    }
}


